package com.example.demo.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: cj
 * @create: 14-08-01 14:47:58
 */
public class MyScheduler {

    public void getScheduler() {
        //创建调度工厂类实例StdSchedulerFactory
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory ();
        try {
            //创建调度器实例Scheduler
            Scheduler scheduler = stdSchedulerFactory.getScheduler ();
            //创建任务详情JobDetail
            JobDetail jobDetail = JobBuilder.newJob ( PrintWordsJob.class )
                    .withIdentity ( "detail01", "group01" )  //定义任务主键 组成唯一标识
                    .withDescription ( "this is a jobDetail" )//描述
                    .usingJobData ( "name", "张三" )//向具体执行的作业类传值方式一
                    .build ();//构建任务
            //向具体执行的作业类传值方式二
//            JobDataMap jobDataMap = jobDetail.getJobDataMap();
//            jobDataMap.put("name", "张三");
            //创建触发器
            Trigger trigger = (Trigger) TriggerBuilder.newTrigger ()
                    .withIdentity ( "trigger1", "group01" )   //组成唯一标识
                    .startAt ( new Date () )             //开始时间
                    .withDescription ( "this is a trigger" )
                    //简单触发器
                    .withSchedule ( SimpleScheduleBuilder.simpleSchedule ()
                            //每隔两秒重复执行3次
                            .withIntervalInSeconds ( 2 ).withRepeatCount ( 3 ) )
                    //表达式调度器（在线生成定时任务表达式：http://cron.qqe2.com/）
                    //每5秒执行一次作业类逻辑代码
                    //.withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ? "))
                    .build ();
            //注入作业类以及触发器到调度器中
            scheduler.scheduleJob ( jobDetail, trigger );
            //启动调度器
            scheduler.start ();

        } catch (SchedulerException e) {
            e.printStackTrace ();
        }

    }

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        // 1、创建调度器Scheduler
        SchedulerFactory schedulerFactory = new StdSchedulerFactory ();
        Scheduler scheduler = schedulerFactory.getScheduler ();
        // 2、创建JobDetail实例，并与PrintWordsJob类绑定(Job执行内容)
        JobDetail jobDetail = JobBuilder.newJob ( PrintWordsJob.class )
                .withIdentity ( "job1", "group1" ).build ();
        // 3、构建Trigger实例,每隔1s执行一次
        Trigger trigger = TriggerBuilder.newTrigger ()
                .withIdentity ( "trigger1", "triggerGroup1" )
                .startNow ()//立即生效
                .withSchedule ( SimpleScheduleBuilder.simpleSchedule ()
                        .withIntervalInSeconds ( 1 )//每隔1s执行一次
                        .repeatForever () ).build ();//一直执行

        //4、执行
        scheduler.scheduleJob ( jobDetail, trigger );
        System.out.println ( "--------scheduler start ! ------------" );
        scheduler.start ();

        //睡眠
        TimeUnit.MINUTES.sleep ( 1 );
        scheduler.shutdown ();
        System.out.println ( "--------scheduler shutdown ! ------------" );


    }
}
